qx.Class.define("lastfmbackup.view.StatusTable", {
  
  extend    : qx.core.Object,
  implement : lastfmbackup.view.IElement,

  properties : {
    
    lastRun : {
      check    : "Number",
      nullable : true,
      apply    : "_applyLastRun"
    },
    
    lastScrobble : {
      check    : "Number",
      nullable : true,
      apply    : "_applyLastScrobble"
    },
    
    scrobbles : {
      check    : "Number",
      nullable : true,
      apply    : "_applyScrobbles"
    },
    
    storageUsed : {
      check    : "Number",
      nullable : true,
      apply    : "_applyStorageUsed"
    }
    
  },
  
  construct : function()
  {
    this.base(arguments);
    
    this._element = q.create(this._getTableTemplate());

    
    this._numberFormatter = new qx.util.format.NumberFormat();
    this._numberFormatter.setMaximumFractionDigits(0);
    
    this._dateFormatter = new qx.util.format.DateFormat();
    
    this._sizeFormatter = new qx.util.format.NumberFormat();
    this._sizeFormatter.setMaximumFractionDigits(0);
    this._sizeFormatter.setPostfix("kB");
  },
  
  members : {
    
    _element : null,
    
    _numberFormatter : null,
    _dateFormatter   : null,
    _sizeFormatter   : null,
    
    
    getElement : function()
    {
      return this._element;
    },
    
    _getTableTemplate : function()
    {
      return [
        "<div class='status-table'>",
          "<table>",
            "<tr>",
              "<td>{0}</td>".format(qx.locale.Manager.tr("Last run")),
              "<td class='last-run' colspan='2'></td>",
            "</tr>",
            "<tr>",
              "<td>{0}</td>".format(qx.locale.Manager.tr("Last scrobble")),
              "<td class='last-scrobble' colspan='2'></td>",
            "</tr>",
            "<tr>",
              "<td colspan='3'><hr/></td>",
            "</tr>",
            "<tr>",
              "<td>{0}</td>".format(qx.locale.Manager.tr("Scrobbles")),
              "<td class='scrobbles'></td>",
              "<td rowspan='2' class='storage-control'></td>",
            "</tr>",
            "<tr>",
              "<td>{0}</td>".format(qx.locale.Manager.tr("Storage used")),
              "<td class='storage-used'></td>",
            "</tr>",
          "</table>",
        "</div>"
      ].join("");
    },
    
    _setValue : function(name, value)
    {
      this._element.find("td.{0}".format(name)).setHtml(value);
    },
    
    getStorageControlElement : function()
    {
      return this._element.find(".storage-control");
    },
    
    _applyLastRun : function(value)
    {
      this._setValue("last-run", this._dateFormatter.format(new Date(value)));
    },
    
    _applyLastScrobble : function(value)
    {
      this._setValue("last-scrobble", this._dateFormatter.format(new Date(value)));
    },
    
    _applyScrobbles : function(value)
    {
      this._setValue("scrobbles", this._numberFormatter.format(value));
    },
    
    _applyStorageUsed : function(value)
    {
      this._setValue("storage-used", this._sizeFormatter.format(value / 1000));
    }
    
  }
  
});